﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CommonTagTime;
using System.IO;
using System.Resources;
public partial class ContactUS : SitePage
{
    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    #endregion

    #region Methods
    private void InitData()
    {
        string validateGroup = "contact";
        btnSubmit.ValidationGroup = validateGroup;
        reqEmail.ValidationGroup = reqComment.ValidationGroup = reqFirstName.ValidationGroup = validateGroup;

        //Init message
        reqFirstName.ErrorMessage = string.Format(GetLang("MSG_REQUIREMENT"), GetLang("Name"));
        reqComment.ErrorMessage = string.Format(GetLang("MSG_REQUIREMENT"), GetLang("Comment"));
        reqEmail.ErrorMessage = string.Format(GetLang("MSG_REQUIREMENT"), GetLang("Email"));
        cusEmail.ErrorMessage = string.Format(GetLang("MSG_VALID"), GetLang("Email"));
        this.Page.Header.Title = string.Format(Resources.Resource.TITLE, GetLang("Contact"));
        btnSubmit.Text = GetLang("Send");
    }
    #endregion

    #region Validate Data
    protected void CheckSPhone(object source, ServerValidateEventArgs args)
    {
        args.IsValid = clsValidate.CheckPhoneValid(args.Value);
    }
    protected void CheckSEmail(object source, ServerValidateEventArgs args)
    {
        args.IsValid = clsValidate.CheckEmailValid(args.Value);
    }
    #endregion

    #region Events, Handle
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (SendMail())
                Response.Redirect(ConfigurationManager.AppSettings["thankyouUrl"].ToString());
            else ltrError.Text = Resources.Resource.MSG_MAIL_ERROR;         

        }
    }
    public bool SendMail()
    {       
        string path = Server.MapPath("~/" + clsConfigData.DEFAULT_MAIL_TEMPLATE_CONTACT_US);
        string[] template = ReadFile(path);

        //clsEmail clsEmail = new clsEmail();
        //clsEmail.Receiver = ;
        //clsEmail.Sender = clsConfigData.DEFAULT_MAIL_FROM;
        //clsEmail.Server = clsConfigData.DEFAULT_MAIL_SERVER;
        //clsEmail.Body = 
        //clsEmail.Subject = ;
        //clsEmail.BodyFormat = enmBodyFormat.HTML;
        ////clsEmail.TimeOut = clsConfigData.DEFAULT_MAIL_TIMEOUT;

        return clsGmail.SendEmail(
                    clsConfigData.DEFAULT_MAIL_FROM, "",
                    string.Format(@template[0]),
                    string.Format(template[1], txtFirstName.Text.Trim(), txtEmail.Text.Trim(), txtDaytimePhone.Text.Trim(), txtComment.Text.Trim()),
                    "");

    }
    public string[] ReadFile(string path)
    {
        string title = string.Empty;
        string body = string.Empty;
        string[] lines = File.ReadAllLines(path);
        for (int i = 0; i < lines.Length; i++)
        {
            if (i == 0)
                title = lines[i].ToString();
            else
            {
                body += lines[i].ToString() + "\r\n";
            }
        }
        string[] result = new string[2];
        result[0] = title;
        result[1] = body;
        return result;
    }
    #endregion
}

